package cn.honglian.org.service.impl;

import cn.honglian.basic.service.impl.BaseServiceImpl;
import cn.honglian.basic.util.MD5Utils;
import cn.honglian.basic.util.SmsUtil;
import cn.honglian.basic.util.StrUtils;
import cn.honglian.org.domain.Employee;
import cn.honglian.org.mapper.EmployeeMapper;
import cn.honglian.org.service.EmployeeService;
import cn.honglian.user.domain.Logininfo;
import cn.honglian.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.util.Password;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public List<Employee> findByIds(Long id) {
        return employeeMapper.findByIds(id);
    }

    @Override
    public void add(Employee employee) {
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 给盐值和密码加密
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(password);
        super.add(employee);
        // 添加角色的id

        Long empid = employee.getId();
        employeeMapper.addBatch(empid, employee.getRoles());
        // 同步到logininfo表内
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(1);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
    }

    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.findById(id);
        logininfoMapper.delete(employee.getLogininfoId());
        employeeMapper.deleteEmpAndRole(id);
        super.delete(id);
    }

    @Override
    public void update(Employee employee) {
        // 同步到logininfo表中
        Employee byId = employeeMapper.findById(employee.getId());
        Logininfo logininfo = logininfoMapper.findById(byId.getLogininfoId());
        logininfo.setUsername(employee.getUsername());
        logininfo.setPassword(employee.getPassword());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        logininfoMapper.update(logininfo);
        // 修改emp的角色时 先删除在添加
        employeeMapper.deleteEmpAndRole(employee.getId());
        employeeMapper.addBatch(employee.getId(), employee.getRoles());
        super.update(employee);

    }

    @Override
    public void batchDelete(List<Long> ids) {
        List<Employee> employees = employeeMapper.findByIdss(ids);
        List<Long> logininfoId = employees.stream().map(Employee::getLogininfoId).collect(Collectors.toList());
        logininfoMapper.batchDelete(logininfoId);
        super.batchDelete(ids);
    }

}
